package helipay.api.util;

import java.util.Random;


/**
 * 
 * @author Kelvin.tie
 *
 */
public class RandomUtil {
	
	/*private final static  String[] ALLCHAR = {"0","1","2","3","4","5","6","7","8","9",
											"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
											"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; */
	/*private final static  String[] ALLCHAR = {"0","1","2","3","4","5","6","7","8","9",
		"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};*/
	private final static  String[] ALLCHAR = {"1","2","3","4","5","6","7","8","9",
		"a","b","c","d","e","f","g","h","j","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
	
	private final static  String[] NUMCHAR = {"0","1","2","3","4","5","6","7","8","9"};
	
	private final static  String[] LETTERCHAR = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
													"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; 
	public final static int TYPE_NUM_AND_CHAR=1;
	public final static int TYPE_CHAR=2;
	public final static int TYPE_CHAR_LOWER=3;
	public final static int TYPE_CHAR_UPPER=4;
	public final static int TYPE_NUM=5;
	
	/**
	  * 生成制定位数，
	  * 1。 数字加字母
	  * 2.大小写字母
	  * 3.小写字母
	  * 4.大写字母
	  * @param randomLenght
	  * @param randomType
	  * @return
	  */
	 public static String RandomCode(int randomLenght,int randomType) {
		 
		 switch (randomType) {
			case TYPE_NUM_AND_CHAR:
				return generateString(randomLenght);
			case TYPE_CHAR:
				return generateCharString(randomLenght);
			case TYPE_CHAR_LOWER:
				return generateLowerCharString(randomLenght);
			case TYPE_CHAR_UPPER:
				return generateUpperCharString(randomLenght);
			case TYPE_NUM:
				return generateNumString(randomLenght);
		
			default:
				throw new RuntimeException("未知的生成格式");
		}
	 }
	 
	/**
	  * 返回一个定长的随机字符串(只包含大小写字母、数字)
	  *
	  * @param length
	  *            随机字符串长度
	  * @return 随机字符串
	  */
	public static String generateString(int length) {
		  StringBuffer sb = new StringBuffer();
		  Random random = new Random();
		  for (int i = 0; i < length; i++) {
		   sb.append(ALLCHAR[random.nextInt(ALLCHAR.length)]);
		  }
		  return sb.toString();
	}
	
	/**
	  * 返回一个定长的随机字符串(只包数字)
	  *
	  * @param length
	  *            随机字符串长度
	  * @return 随机字符串
	  */
	public static String generateNumString(int length) {
		  StringBuffer sb = new StringBuffer();
		  Random random = new Random();
		  for (int i = 0; i < length; i++) {
		   sb.append(NUMCHAR[random.nextInt(NUMCHAR.length)]);
		  }
		  return sb.toString();
	}

	 /**
	  * 返回一个定长的随机纯字母字符串(只包含大小写字母)
	  *
	  * @param length
	  *            随机字符串长度
	  * @return 随机字符串
	  */
	public static String generateCharString(int length) {
		  StringBuffer sb = new StringBuffer();
		  Random random = new Random();
		  for (int i = 0; i < length; i++) {
		   sb.append(LETTERCHAR[random.nextInt(LETTERCHAR.length)]);
		  }
		  return sb.toString();
	}

	/**
	  * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
	  *
	  * @param length
	  *            随机字符串长度
	  * @return 随机字符串
	  */
	public static String generateLowerCharString(int length) {
		 return generateCharString(length).toLowerCase();
	}

	 /**
	  * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
	  *
	  * @param length
	  *            随机字符串长度
	  * @return 随机字符串
	  */
	public static String generateUpperCharString(int length) {
		 return generateCharString(length).toUpperCase();
	}

	 /**
	  * 生成一个定长的纯0字符串
	  *
	  * @param length
	  *            字符串长度
	  * @return 纯0字符串
	  */
	public static String generateZeroString(int length) {
		  StringBuffer sb = new StringBuffer();
		  for (int i = 0; i < length; i++) {
		   sb.append('0');
		  }
		  return sb.toString();
	}

	/**
	  * 根据数字生成一个定长的字符串，长度不够前面补0
	  *
	  * @param num
	  *            数字
	  * @param fixdlenth
	  *            字符串长度
	  * @return 定长的字符串
	  */
	 public static String toFixdLengthString(long num, int fixdlenth) {
	  StringBuffer sb = new StringBuffer();
		  String strNum = String.valueOf(num);
		  if (fixdlenth - strNum.length() >= 0) {
		   sb.append(generateZeroString(fixdlenth - strNum.length()));
		  } else {
		   throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
		     + "的字符串发生异常！");
		  }
		  sb.append(strNum);
		  return sb.toString();
	 }
}